package bsts.controller;

import java.util.ArrayList;

import bsts.model.Plan;

public class PlanController {

	private ArrayList<Plan> plans;
	private IOController ioController;

	public PlanController() {
		ioController = new IOController();
		this.plans = ioController.RetrievePlans();
	}

	public boolean UpdatePlan(Plan plan) {
		for (int i = 0; i < plans.size(); i++) {
			Plan comp = plans.get(i);
			if (comp.getPlanId() == plan.getPlanId()) {
				// Remove from Arraylist
				plans.remove(i);
				// Insert the update object to Arraylist
				plans.add(plan);
				// Update file
				return ioController.UpdatePlans(this.plans);
			}
		}// end of loop
		return false;
	}// end of function

	public ArrayList<Plan> getPlans() {
		return plans;
	}

	public Integer getCurrent_PlanID() {
		Integer temp_PlanID;
		ArrayList<Plan> tempPlans = new ArrayList<Plan>();
		ioController = new IOController();

		tempPlans = ioController.RetrievePlans();

		if (tempPlans == null) {
			temp_PlanID = new Integer(0);
		} else {
			temp_PlanID = new Integer(10000 + tempPlans.size());
		}

		return temp_PlanID;
	}

	public Integer getNext_PlanID() {
		return getCurrent_PlanID() + 1;
	}

	public Plan GetPlan(long plan_id) {
		for (int i = 0; i < this.plans.size(); i++) {
			Plan comp = plans.get(i);
			if (comp.getPlanId() == plan_id) {
				return comp;
			}
		}// end of loop
		return null;
	}// end of function
}
